package dao;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-11-07
 * Time: 23:09
 */
public class BlogDao {
    public void insert(Blog blog) throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        DButi dButi = new DButi();
        try {
            connection = dButi.getconnect();
            String sql = "insert into blog values(null, ?, ?, ?, now())";
            statement = connection.prepareStatement(sql);
            statement.setString(1, blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setInt(3, blog.getUserId());
            statement.executeUpdate();
        }
        finally{
            dButi.close(connection, statement, null);
        }
    }

    public Blog getblog(int port) throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        DButi dButi = new DButi();
        try {
            connection = dButi.getconnect();
            String sql = "select * from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, port);
            resultSet = statement.executeQuery();
            if(!resultSet.next()){
                return null;
            }
            Blog blog = new Blog();
            blog.setTitle(resultSet.getString("title"));
            blog.setBlogId(resultSet.getInt("blogId"));
            blog.setUserId(resultSet.getInt("userId"));
            blog.setContent(resultSet.getString("content"));
            blog.setPostTime(resultSet.getTimestamp("postTime"));
            return blog;
        }finally {
            dButi.close(connection, statement, resultSet);
        }
    }

    public List<Blog> getblogs() throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Blog> lists = new ArrayList<>();
        DButi dButi = new DButi();
        try {
            connection = dButi.getconnect();
            String sql = "select * from blog order by postTime desc";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                Blog blog = new Blog();
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                lists.add(blog);
            }
            return lists;
        }finally {
            dButi.close(connection,statement,resultSet);
        }
    }

}
